import React from 'react';

export default () => {

    return (
        <div>
            LazyMan.jsx
        </div>
    );
};

class LazyMan {
    constructor(name) {
        this.name = name;
        this.queue = []; // 队列
        // console.log(`Hi this is ${name}`);
        // 延迟调度
        setTimeout(() => {
            this.next();
        }, 0);
    }
    next(name) {
        const now = this.queue.shift();  // 取出第一个
        now && now();
    }

    register(fn, isFirst) {
        if (isFirst) {
            this.queue.unshift(); // 插入到头部
        } else {
            this.queue.push(fn);
        }
    }

    eat(food) {
        const _eat = () => {
            console.log(`eat${food}`);
            this.next('吃东西');
        };
        this.register(_eat);
        return this;
    }

    sleep(s, isFirst = false) {
        const time = s * 1000;
        const _sleep = () => {
            console.log(`${time}秒钟醒来`);
            setTimeout(() => {
                this.next();
            }, time);
        };
        this.register(_sleep, isFirst);
        return this;
    }
}

function generateLazy(name) {
    return new LazyMan(name);
}

generateLazy('小名').eat('饭饭').sleep(2).eat('在吃点吧');